<template>
<el-drawer :title="formDrawer.title" :visible.sync="formDrawer.visible" direction="rtl" custom-class="drawer-right-side" :before-close="closeDrawer" :append-to-body="true">
    <el-form class="ruleForm" ref="ruleForm" :model="ruleForm" :rules="rules" label-position="left" label-width="100px">
        <el-form-item label="餐标" prop="price" required>
            <el-input type="number" v-model="ruleForm.price" placeholder="" @input="setTotal"></el-input>
        </el-form-item>
        <el-form-item label="" prop="amount" required>
            <el-input type="number" v-model="ruleForm.amount" placeholder="" @input="setTotal"></el-input>
        </el-form-item>
        <el-form-item label="优惠金额" prop="discount">
            <el-input type="number" v-model="ruleForm.discount" placeholder="" @input="setTotal"></el-input>
        </el-form-item>
        <el-form-item label="金额小计" prop="total" required>
            <el-input type="number" v-model="ruleForm.total" placeholder="" readonly></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submit">提交并保存</el-button>
        </el-form-item>
    </el-form>
</el-drawer>
</template>

<script>
import {
    updateServiceApi
} from '@/api/order/service';

const ruleForm = {
    price: '',
    amount: '',
    discount: '',
    total: '',
};

export default {
    data() {
        return {
            formDrawer: {
                title: "",
                visible: false,
            },
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                price: [{
                    required: true,
                    message: "请录入餐标",
                }],
                amount: [{
                    required: true,
                    message: "请录入",
                }],
                total: [{
                    required: true,
                    message: "小计不能为空",
                }],
            },
        }
    },
    methods: {
        showDrawer(row) {
            this.formDrawer.visible = true;
            let titles = {
                1: '加桌',
                2: '减桌',
                3: '加菜',
                4: '减菜'
            };
            this.formDrawer.title = titles[row.type];
            this.ruleForm = Object.assign({}, row);
        },
        closeDrawer() {
            this.formDrawer.visible = false;
        },
        setTotal() {
            let tablePrice = parseFloat(this.ruleForm.price || 0);
            let tableAmount = parseFloat(this.ruleForm.amount || 0);
            let discount = parseFloat(this.ruleForm.discount || 0);
            let total = (tablePrice * tableAmount - discount).toFixed(2);
            this.ruleForm.total = total;
        },
        submit() {
            this.$refs['ruleForm']
                .validate(valid => {
                    if (valid) {
                        let params = this.ruleForm;
                        updateServiceApi(params).then((res) => {
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                this.$emit("doRefresh", this.ruleForm.order_id, this.ruleForm.type);
                                this.closeDrawer();
                            } else {
                                this.$message.error(res.msg);
                            }
                        }).catch((err) => {

                        });
                    }
                })
        }
    }
}
</script>

<style>
    
    </style>
